package lab11.com.acme.domain.account;

import lab11.com.acme.exceptions.NotEnoughFundsException;
import lab11.com.acme.exceptions.OverDraftLimitExceededException;


public class CheckingAccount extends AbstractAccount {

	private static final String AM_NOT_BE_NEG = "Amount can not be negative";
	private double overdraft;

	public CheckingAccount(final int id, final double amount,
			final double overdraft) {
		super(id, amount);
		this.overdraft = overdraft;
	}

	@Override
	public void withdraw(final double amount) throws NotEnoughFundsException {
		if (amount < 0) {
			throw new IllegalArgumentException(AM_NOT_BE_NEG);
		}
		
		if (this.balance < amount) {
			double overdraftNeeded = amount - this.balance;
			if (overdraft < overdraftNeeded) {
				throw new OverDraftLimitExceededException(overdraft);
			} else {
				this.balance = 0.0;
				overdraft -= overdraftNeeded;
			}
		} else {
			this.balance = this.balance - amount;
		}
	}

	@Override
	public void deposit(final double amount) {
		if (amount > 0) {
			this.balance += amount;
		} else {
			throw new IllegalArgumentException(AM_NOT_BE_NEG);
		}
	}


	public AccountType getAccountType() {
		return AccountType.CHECKING;
	}

	public double getOverdraft() {
		return overdraft;
	}
}
